<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, i) in goodsList" @click="gotoDetial(goods)" :key="i">
        <my-goods :goods = "goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
            // 商品列表的数据
            goodsList: [],
            // 总数量，用来实现分页
            total: 0,
            // 默认的空图片
            isloading: false
      }
    },
    onLoad(options) {
      // 将页面参数转存到 this.queryObj 对象中
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // console.log(this.queryObj.query)
      // console.log(this.queryObj.cid)
      this.getGoodsList()
    },
      methods: {
        // 获取商品列表数据的方法
        async getGoodsList(cb) {
          //关闭节流阀
          this.isloading = true
          // 发起请求
          const { data: res } = await uni.$http.get('https://api-hmugo-web.itheima.net/api/public/v1/goods/search', this.queryObj)
          // console.log(res)
          if (res.meta.status !== 200) return uni.$showMsg()
          this.isloading = false
            // 只要数据请求完毕，就立即按需调用 cb 回调函数
            cb && cb()
          // 为数据赋值
          this.goodsList = [...this.goodsList,...res.message.goods]
          this.total = res.message.total
          //开启节流阀
        },
        gotoDetial(item){
          
          uni.navigateTo({
            url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
          })
        }
      },
      onReachBottom(){
        if( this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('没有数据惹！')
        if(this.isloading) return
        this.queryObj.pagenum += 1
        this.getGoodsList()
        
      },
      onPullDownRefresh() {
         this.queryObj.pagenum=1
         this.isloading=false
         this.goodsList=[]
         this.getGoodsList(() => uni.stopPullDownRefresh())
      }
  }
</script>

<style lang="scss">

</style>
